package com.wc.alorithm_blue_bridge._二分.备份比赛数据;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/29 13:55
 * @description
 * https://www.lanqiao.cn/problems/20274/learning/?contest_id=250
 */
public class Main {
    /**
     * 思路：
     * 二分答案, 查看每天这么多时间是否够
     * 前缀和来支持二分
     * 每天干活, 也可以二分
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] a = new int[N], b = new int[N];
    static long[] sa = new long[N], sb = new long[N];
    static int n, t;

    public static void main(String[] args) {
        n = sc.nextInt();
        t = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
        for (int i = 1; i <= n; i++) sa[i] = sa[i - 1] + a[i];
        for (int i = 1; i <= n; i++) sb[i] = sb[i - 1] + b[i];
        int l = 1, r = 3600;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        int res = -1;
        if (check(l)) res = l;
        out.println(res);
        out.flush();
    }

    static boolean check(int x) {
        // j 表示当天正要做, time 表示当天剩余的时间
        int j = 1, time = x;
        for (int i = 1; i <= t; i++) {
            // 防止当天都不够做一个 j 表示当天要正要做这个
            int l = j - 1, r = n;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                // 做到 mid, mid - 1 的 b
                long sum = sa[mid] - sa[j - 1] + sb[mid - 1] - sb[j - 1];
                if (sum <= time) l = mid;
                else r = mid - 1;
            }

            long sum = sa[l] - sa[j - 1] + sb[l - 1] - sb[j - 1];
            time -= sum;
            // 将 l 剩余的 b 做掉, 做不掉就放到第二天做
            time = x + Math.min(0, time - b[l]);
            j = l + 1;
            // j 个全部做完就结束了
            if (j > n) break;
        }

        return j > n;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

